package com.smartleanx.module.hrm.app.empRisk;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.smartleanx.framework.common.util.collection.CollectionUtils;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeContractDO;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeDO;
import com.smartleanx.module.hrm.dal.dataobject.risk.RiskTypeDO;
import com.smartleanx.module.hrm.enums.ErrorCodeConstants;
import com.smartleanx.module.hrm.eums.employee.EmploymentFormEnum;
import com.smartleanx.module.hrm.eums.risk.RiskLevelEnum;
import com.smartleanx.module.hrm.eums.risk.RiskTypeEnum;
import com.smartleanx.module.hrm.intergration.FileServiceProvider;
import com.smartleanx.module.hrm.service.employee.EmployeeContractService;
import com.smartleanx.module.hrm.service.employee.EmployeeService;
import com.smartleanx.module.hrm.service.risk.RiskService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import static com.smartleanx.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 劳动合同的续签
 *
 * @author tengcj
 * @since 2024/6/11
 */
@Component
public class RiskContractRenewalHandleService implements EmployeeRiskHandle {

    @Resource
    private EmployeeService employeeService;

    @Resource
    private EmployeeContractService employeeContractService;

    @Resource
    private RiskService riskService;

    @Resource
    private FileServiceProvider fileServiceProvider;

    public static final long RISK_ID = RiskTypeEnum.LABOR_CONTRACT_EXPIRATION.getCode();

    @Override
    public Long type() {
        return RISK_ID;
    }

    @Override
    public void handleEmployeeRisk(Set<Long> employeeIds) {
        List<EmployeeDO> employeeList = employeeService.getEmployeeList(employeeIds);
        if (CollUtil.isEmpty(employeeList)) {
            return;
        }

        RiskTypeDO riskType = riskService.getRiskType(RISK_ID);
        if (riskType == null) {
            throw exception(ErrorCodeConstants.RISK_TYPE_NOT_EXISTS);
        }

//        Map<Long, List<EmployeeContractDO>> processContractMap = CollectionUtils.convertMultiMap(
//                employeeContractService.getInProgressContractList(employeeIds), EmployeeContractDO::getEmployeeId
//        );

        Map<Long, List<EmployeeContractDO>> contractMap = CollectionUtils.convertMultiMap(
                employeeContractService.getEmployeeContractList(employeeIds), EmployeeContractDO::getEmployeeId
        );

        // FileRefQueryDTO fileRefQueryDTO = new FileRefQueryDTO();
        // fileRefQueryDTO.setBizType(BizTypeEnum.EMPLOYEE_LABOR_CONTRACT.getValue());
        // fileRefQueryDTO.setBizFileTypes(Collections.singletonList(BizFileTypeEnum.CONTRACT_IN_SERVICE.getCode()));
        // fileRefQueryDTO.setBizRecordIds(progressContractList.stream().map(EmployeeContractDO::getId).collect(Collectors.toSet()));
        // List<FileRefRespDTO> fileRefList = fileServiceProvider.getFileRef(fileRefQueryDTO);
        // Map<Long, List<FileRefRespDTO>> fileRefMap = CollectionUtils.convertMultiMap(fileRefList, FileRefRespDTO::getBizRecordId);

        emp:
        for (EmployeeDO employeeDO : employeeList) {
            if (Objects.equals(EmploymentFormEnum.INFORMAL.getCode(), employeeDO.getEmploymentForm())) {
                riskService.deleteRisk(employeeDO.getId(), RISK_ID);
                continue;
            }
            List<EmployeeContractDO> contractList = contractMap.get(employeeDO.getId());
            if (CollUtil.isEmpty(contractList)) {
                riskService.deleteRisk(employeeDO.getId(), RISK_ID);
                return;
            }
            EmployeeContractDO first = CollectionUtils.findFirst(contractList, Objects::nonNull);
            // 终止的合同不提示合同相关风险
            if (first == null || first.getTerminationStatus()) {
                riskService.deleteRisk(employeeDO.getId(), RISK_ID);
                return;
            }
            LocalDateTime endTime = first.getEndTime();
            if (endTime == null) {
                riskService.deleteRisk(employeeDO.getId(), RISK_ID);
                continue;
            }
            long between = ChronoUnit.DAYS.between( LocalDate.now(),endTime.toLocalDate());
            if (between <= 60) {
                List<EmployeeContractDO> contracts = contractMap.get(employeeDO.getId());
                contract:
                for (EmployeeContractDO contract : contracts) {
                    if (endTime.isBefore(contract.getStartTime())) {
                        riskService.deleteRisk(employeeDO.getId(), RISK_ID);
                        continue emp;
                    }
                }
                riskService.addRiskToEmployee(employeeDO.getId(), RISK_ID, RiskLevelEnum.MAJOR.getLevel());
            }else{
                riskService.deleteRisk(employeeDO.getId(), RISK_ID);
            }
        }
    }

    public static void main(String[] args) {
        LocalDateTime endTime = LocalDateTime.parse("2027-09-22T00:00:00");
        long  between = ChronoUnit.DAYS.between( LocalDate.now(),endTime.toLocalDate());
        System.err.println(between);
    }

}
